package bmptest.bmp.common.realm;

import bmptest.bmp.controller.login.LoginController;
import org.apache.shiro.authc.AuthenticationInfo;
import org.apache.shiro.authc.AuthenticationToken;
import org.apache.shiro.authc.SimpleAuthenticationInfo;
import org.apache.shiro.authz.AuthorizationInfo;
import org.apache.shiro.realm.AuthorizingRealm;
import org.apache.shiro.subject.PrincipalCollection;
import org.apache.shiro.util.ByteSource;


/**
 * @author lyq
 * @date 2021/9/8 11:10
 * 登录realm实现
 */
public class LoginRealm extends AuthorizingRealm {

    //授权
    @Override
    public AuthorizationInfo doGetAuthorizationInfo(PrincipalCollection principals){
        return null;
    }

    //认证
    @Override
    public AuthenticationInfo doGetAuthenticationInfo(AuthenticationToken token){
        LoginController loginController = new LoginController();
        String password = loginController.getPasswordLogin();
        //在token中获取用户名
        String principal = (String)token.getPrincipal();
        //参数1:返回数据库中正确用户名 参数2:md5+盐密码+散列 参数3:提供当前realm名字
        SimpleAuthenticationInfo simpleAuthenticationInfo = new SimpleAuthenticationInfo(principal,password,ByteSource.Util.bytes("fdiop$ewr"),this.getName());
        return simpleAuthenticationInfo;
    }

}
